<template>
  <div class="xtx-category" @mouseleave="categoryId = ''">
    <!-- 分类菜单 -->
    <ul class="xtx-category__menu">
      <li
        class="xtx-category-menu__item"
        :class="{ active: categoryId === item.id }"
        @mouseenter="categoryId = item.id"
        v-for="item in menuList"
        :key="item.id"
      >
        <router-link :to="`/category/${item.id}`">{{ item.name }}</router-link>
        <template v-if="item.children.length !== 0">
          <router-link
            :to="`/category/sub/${child.id}`"
            v-for="child in item.children"
            :key="child.id"
            >{{ child.name }}</router-link
          >
        </template>
        <template v-else>
          <Skeleton
            width="60px"
            height="18px"
            margin-right="5px"
            bgColor="rgba(255,255,255,0.2)"
            :animate="true"
          />
          <Skeleton
            width="50px"
            height="18px"
            bgColor="rgba(255,255,255,0.2)"
            :animate="true"
          />
        </template>
      </li>
    </ul>
    <!-- 弹层 -->
    <div class="xtx-category-layer" v-if="currCategory">
      <h4>
        {{ currCategory.id === 'brand' ? '品牌' : '分类' }}推荐
        <small>根据您的购买或浏览记录推荐</small>
      </h4>
      <ul
        v-if="
          currCategory &&
          currCategory.goods &&
          Array.isArray(currCategory.goods) &&
          currCategory.goods.length
        "
      >
        <li v-for="goods in currCategory?.goods" :key="goods.id">
          <router-link to="#">
            <img :src="goods.picture" alt="" />
            <div class="info">
              <p class="name">{{ goods.name }}</p>
              <p class="desc">{{ goods.desc }}</p>
              <p class="price"><i>¥</i>{{ goods.price }}</p>
            </div>
          </router-link>
        </li>
      </ul>
      <ul
        v-else-if="
          currCategory &&
          currCategory.brands &&
          Array.isArray(currCategory.brands) &&
          currCategory.brands.length
        "
      >
        <li class="brand" v-for="brand in currCategory.brands" :key="brand.id">
          <router-link to="#">
            <img :src="brand.picture" alt="" />
            <div class="info">
              <p class="place">{{ brand.place }}</p>
              <p class="name">{{ brand.name }}</p>
              <p class="desc">{{ brand.desc }}</p>
            </div>
          </router-link>
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { computed, reactive, ref } from 'vue'
import { useCategoryStore } from '@/store/modules/category'
import { hotBrandAPI } from '@/api/home'
import type { HotBrand } from '@/api/home/types.d'
import Skeleton from '@/components/Skeleton.vue'
defineOptions({
  name: 'Category'
})

/** 品牌数据 */
const brand = reactive({
  id: 'brand',
  name: '品牌',
  picture: '',
  children: [
    {
      id: 'brand-children',
      name: '品牌推荐',
      picture: '',
      children: [],
      goods: []
    }
  ],
  goods: [],
  brands: [] as HotBrand[]
})

/** 分类id */
const categoryId = ref()

/** 分类仓库 */
const categoryStore = useCategoryStore()
/** 从分类仓库中解构出来分类列表 */
const { categoryList } = storeToRefs(categoryStore)

/** 菜单分类列表 */
const menuList = computed(() => {
  const list = categoryList.value.map((category) => {
    return {
      id: category.id,
      name: category.name,
      picture: category.picture,
      goods: category.goods,
      children: category.children?.slice(0, 2),
      brands: [] as HotBrand[]
    }
  })
  /** 末尾追加品牌数据到菜单列表中 */
  list.push(brand)
  return list
})

/** 获取当前分类数据 */
const currCategory = computed(() => {
  return menuList.value.find((category) => category.id === categoryId.value)
})

console.log('currCategory', currCategory.value)

/** 获取热门品牌数据 */
function getHotBrandData(): void {
  hotBrandAPI(6)
    .then((res) => {
      brand.brands = res
    })
    .catch((error) => {
      console.log(error)
    })
}
getHotBrandData()
</script>

<style lang="scss" scoped>
.xtx-category {
  position: relative;
  z-index: 99;
  width: 250px;
  height: 500px;
  background-color: rgba(0, 0, 0, 0.8);

  &-menu {
    &__item {
      padding-left: 40px;
      height: 50px;
      line-height: 50px;

      &:hover,
      &.active {
        background-color: $xtx-color;
      }

      a {
        margin-right: 4px;
        color: $basic-white;
        font-size: 14px;

        &:first-child {
          font-size: 16px;
        }
      }

      .skeleton {
        animation: fade 1s linear infinite alternate;
      }
    }
  }

  &-layer {
    position: absolute;
    top: 0;
    left: 250px;
    padding-left: 15px;
    width: 990px;
    height: 500px;
    background-color: rgba(255, 255, 255, 0.8);

    h4 {
      line-height: 80px;
      font-size: 20px;
      font-weight: 400;

      small {
        color: #666;
        font-size: 16px;
      }
    }

    ul {
      display: flex;
      flex-wrap: wrap;

      li {
        margin: 0 15px 15px 0;
        width: 310px;
        height: 120px;
        background-color: $basic-white;
        border: solid 1px #eee;
        border-radius: 4px;

        a {
          display: flex;
          align-items: center;
          padding: 10px;
          height: 100%;

          &:hover {
            background-color: #e3f9f4;
          }

          img {
            width: 95px;
            height: 95px;
          }

          .info {
            padding-left: 10px;
            width: 190px;
            white-space: nowrap;
            line-height: 24px;

            p {
              overflow: hidden;
              width: 190px;
              text-overflow: ellipsis;
            }

            .name {
              color: #666;
            }

            .desc {
              color: #999;
              font-size: 14px;
            }

            .price {
              color: $price-color;
              font-size: 22px;
            }
          }
        }

        &.brand {
          height: 180px;

          a {
            align-items: flex-start;
          }

          img {
            width: 120px;
            height: 160px;
          }

          .info {
            p {
              margin-top: 8px;
            }

            .place {
              color: #999;
            }
          }
        }
      }
    }
  }
}

@keyframes fade {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}
</style>
